package com.region.integral.common_user.consume.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.consume.dao.IBuildGroup;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.BuildGroup;
import com.region.integral.common_user.xml.dto.CommentGoods;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.CommonUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iBuildGroup")
public class BuildGroupImpl extends BaseHibernate implements IBuildGroup {

	@Override
	public void buildGroup(int userid, IntegralData integralData) throws IntegralException {

		CUIEntity cuiEntity = getCUIEntity(userid);
		if (cuiEntity != null) {
			int currIntegral = cuiEntity.getCurrIntegral();
			
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<BuildGroup> buildGroups = regulation.getBuildgroup();
			for (BuildGroup buildGroup : buildGroups) {
				int ruleIntegral = buildGroup.getIntegral();
				if(currIntegral >= Math.abs(ruleIntegral)){
					int totalIntegral = currIntegral + ruleIntegral;
					cuiEntity.setCurrIntegral(totalIntegral);
					hibernateTemplate.update(cuiEntity);
					hibernateTemplate.save(CommonUserLogUtil.log(userid, ruleIntegral, "buildGroup"));
					integralData.setSuccee(true);
				}
				else{
					integralData.setSuccee(false);
				}
			}
			
		}
		
	}

}
